fog - Discover the "Fog Index" of Your Writing

Mark Leighton Fisher on 2006-02-24T20:44:59

I was writing an email at work one day, but got to thinking – is this too wordy? I finally remembered that prose readability is often measured with something called a "fog index" (how foggy is your writing?)

A search on Google turned up Merlyn's article "Discovering incomprehensible documentation" for his Nov 2000 Linux Magazine column. His article describes "fog indexing" his manpages using Lingua::EN::Fathom (one of the wonders of CPAN), a module for computing the fog index of a piece of text. But I wanted something more – a simple, general-purpose utility.

So, here is "fog", a simple command-line program for discovering the fog index of text. fog (a thin, tasty wrapper around Lingua::EN::Fathom) can work on either a string, standard input, or a set of files. It is a little rough around the edges, so any suggestions are welcomed.

Here is the source for fog:

#!/usr/bin/perl -w
# List various measures of readability -- "fog indexes".
#
# Just a thin, tasty wrapper around Lingua::EN::Fathom.


# ------ pragmas
use warnings;
use strict;
use Lingua::EN::Fathom;


# ------ variables
my $file_count = 0;                     # count of files to analyze
my $file_name  = "";			# current filename
my $fog_index  = "";                    # fog indexing object (a Lingua::EN::Fathom)
my $ifh        = undef;                 # input file handle
my $input      = "";                    # string to analyze
my @input      = "";                    # array of strings to analyze
my $more_files = 0;                     # TRUE when more than one file to analyze


# ------ process command-line arguments
if (@ARGV < 1) {
    die "usage: fog -|-f FILE1 ...|STRING\n"
}
if ($ARGV[0] eq "-") {
no strict 'subs';
    $ifh = *STDIN;
    @input = <$ifh>;
    $input = join("\n", @input);
    $file_count = 1;
use strict;
} elsif ($ARGV[0] eq "-f" && @ARGV >= 2) {
    $file_count = scalar(@ARGV) - 1;
    if ($file_count > 1) {
        $more_files++;
    }
    shift;
    open($ifh, $ARGV[0]) || die "cannot open $ARGV[0]: $!\n";
    @input = <$ifh>;
    close($ifh);
    $input = join("\n", @input);
    $file_name = $ARGV[0];
} else {
    $input = $ARGV[0];
    $file_count = 1;
}


while ($file_count > 0) {
    $fog_index = new Lingua::EN::Fathom;
    $fog_index->analyse_block($input, 1);
    if ($more_files > 0) {
        print $file_name, ":\n";
    }
    print $fog_index->report, "\n";

    $file_count--;
    if ($file_count > 0) {
        shift;
	$file_name = $ARGV[0];
        open($ifh, $file_name) || die "cannot open $file_name: $!\n";
        @input = <$ifh>;
        close($ifh);
        $input = join("\n", @input);
    }
}